#ifndef _SYSCORE_X
#define _SYSCORE_X

#include "timer.h"


#define MODULE_ID_BASE 0x0100
#define MAX_MODULE_NUM 0x20

#define QUEUE_NUM_PER_MQ 3
#define QUEUE_SIZE 256

#define MAX_LOGIDENT_LEN 15
typedef struct tagQUEUE {
    SYS_lock_t lock;
    void **storage;
    U32 get;
    U32 put;
    U32 size;
    U32 reserved;
} QUEUE,*PQUEUE;

typedef struct tagMQUEUE {    
    SYS_sema_t  sema;
    SYS_lock_t  lock;
    U8 queue_idx;
    QUEUE queue[QUEUE_NUM_PER_MQ];
    PTR  storage[QUEUE_NUM_PER_MQ*QUEUE_SIZE];
}MQUEUE,*PMQUEUE;

typedef struct tagMEMARRAY {
    PTR * volatile freeblk;
    void *storage;
    void *end_of_storage;    
    U32 blksz;
    U32 blkcnt;
    SYS_lock_t lock;
    U32 refcount;
} MEMARRAY, *PMEMARRAY;


typedef struct tagMODDESC{
BOOL valid;
U16 mod_id;
U32 tickPeriod;
PMQUEUE pMQueue;
PTIMERCONTEXT pTimerContext;
PTIMERINFO pTicker;
char logIdent[MAX_LOGIDENT_LEN+1];
}MODDESC,*PMODDESC;


typedef struct tagMODINFO{
U16 mod_num;
U32 reg_mask;
MODDESC mod[MAX_MODULE_NUM]; 
}MODINFO,*PMODINFO;


#define NUM_SYS_TIMERS			250
#define NUM_MOD_TIMERS          32
//#define SYSTIMER_INT_INTERVAL   10
//#define SYSTIMER_INT_RESOLUTION 10
#define SYSTIMER_RESOLUTION_MS     100
#define SYSTIMER_RESOLUTION_NS  (SYSTIMER_RESOLUTION_MS*1000*1000)
#define SYS_TIMERTASK_PRIORITY 90


typedef struct tagMODTIMER {
    TIMERCONTEXT context;
    MEMARRAY timerPart;
    TIMERINFO storage[NUM_MOD_TIMERS];    
} MODTIMER;

typedef struct tagSYSTIMER {
    TIMERCONTEXT context;
    MEMARRAY timerPart;
    TIMERINFO storage[NUM_SYS_TIMERS];    
} SYSTIMER;


extern RESULT SYS_initMemArray(PMEMARRAY pArray, void *storage, U32 blksz, U32 blkcnt);
extern RESULT SYS_destoryMemArray(PMEMARRAY pArray);
extern RESULT SYS_destoryMemArray(PMEMARRAY pArray);
extern void* SYS_allocMemArray(PMEMARRAY pArray );
extern RESULT SYS_freeMemArray(PMEMARRAY pArray, void *pBlock);
extern void SYS_log(int priority, const char* fmt, ... );




#endif
